﻿using System.Linq;
using System.Web.Mvc;

using SCB.Presentation.Models;
using SCB.Presentation.Helpers;

namespace SCB.Presentation.Controllers
{
    [HandleError]
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        public ActionResult Search(string searchText)
        {
            return View();
        }

        [HttpPost]
        public JsonResult Search(string searchText, int maxResults)
        {
            var result = ProductService.AllProducts.Where(p => p.Title.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Take(maxResults).
                Select(s => new SearchResultModels() { Name = s.Title, View = Utilities.GetProductDetailView(s.Id, s.Title) }).ToList();
            result.AddRange(
                CategoryService.AllCategories.Where(c => c.Name.StartsWith(searchText, System.StringComparison.CurrentCultureIgnoreCase)).Take(maxResults).
                Select(s => new SearchResultModels(){Name = s.Name, View = Utilities.GetCategoryDetailView(s.Id, s.Name)}).ToList());

            result = result.OrderBy(s => s.Name).Take(maxResults).ToList();

            return Json(result);
        }
    }
}